package com.bing.controller;

import com.bing.bean.Person;
import com.bing.bean.TestEL;
import com.bing.bean.TestValue;
import com.bing.mvc.validator.Second;
import com.bing.mvc.validator.Thrid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kaibb on 2014/9/30.
 */

@Controller
@RequestMapping("hello")
public class HelloController {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired()
    private TestEL testEL;

    @Autowired()
    private TestValue testValue;

    @RequestMapping("/{input}")
    public ModelAndView index(@PathVariable String input) {
        logger.info("hello spring mvc:{}", input);
        ModelAndView modelAndView = new ModelAndView("helloFtl");
        Map<String, String> map = new HashMap<String, String>();
        map.put("title", "Freemarker 测试");
        map.put("world", "freemarker");
        map.put("input", input);
        modelAndView.addObject("model", map);
        return modelAndView;
    }


    @RequestMapping(value = "/test/el", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseBody
    public TestEL testEL() {
        return testEL;
    }

    @RequestMapping(value = "/xmlOrJson/{name}", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseBody
    public Person getPerson(@PathVariable String name) {
        Person person = new Person();
        person.setName(name);
        person.setAge(21);
        return person;
    }

    @RequestMapping("i18n")
    public String i18n() {
        return "i18n";
    }

    @RequestMapping(value = "file")
    @ResponseBody
    public String updateFile(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            file.transferTo(new File("d:/temp/" + file.getOriginalFilename()));
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String addPerson(@Valid @ModelAttribute("user") Person user, BindingResult result) {
        logger.info("add Person: {}, {}", user, result.hasErrors());
        return "addPerson";
    }

    @RequestMapping(value = "view/addPerson2", method = RequestMethod.POST)
    public String addPerson2(@Validated({Second.class, Thrid.class}) @ModelAttribute("user") Person user, BindingResult result) {
        logger.info("add Person2: {}, {}", user, result.hasErrors());
        return addPersonView("addPerson2");
    }

    @RequestMapping(value = "view/{page}", method = RequestMethod.GET)
    public String addPersonView(@PathVariable String page) {
        return page;
    }

    @RequestMapping(value = "value", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseBody
    public TestValue veiwValue() {
        return testValue;
    }


    @RequestMapping(value = "getvalue", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseBody
    public TestValue getValue(@RequestBody TestValue a) {
        logger.info("get value:{}", a);

        return testValue;
    }


    @RequestMapping(value = "error", method = RequestMethod.GET)
    @ResponseBody
    public String error() {
        String a = null;
        a.startsWith("aa");
        return a;
    }

}
